package com.zlzxm.shoesmarket.view.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.globle.UserManager;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.activity.manage.ActivityIndex;
import com.zlzxm.shoesmarket.view.activity.manage.bundle.BundleGoodsInfo;
import com.zlzxm.shoesmarket.view.adapter.ADRepositoryGoods;
import com.zlzxm.shoesmarket.view.adapter.ADRepositoryInfo;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityRepositoryGoods;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityRepositoryInfo;
import com.zlzxm.shoesmarket.view.window.wheelpick.EntityOption;
import com.zlzxm.shoesmarket.view.window.wheelpick.WindowWheelPick;
import com.zlzxm.shoesmarket.viewmodel.VMRepository;

import java.util.List;

/**
 * CREATE BY zlzxm ON 4/7/21
 */
public class FMNavigationRepository extends BaseFragment implements View.OnClickListener, OnItemClickListener {

    private static final String TAG = "FMNavigationRepository";

    private VMRepository vmRepository;

    private RecyclerView rvInfo,rvGoods;

    private final ADRepositoryInfo adRepositoryInfo =  new ADRepositoryInfo(null);

    private final ADRepositoryGoods adRepositoryGoods = new ADRepositoryGoods(null);

    private TextView tvTitle;

    private ImageView ivAdd;

    //<editor-fold defaultState="collapsed" desc="liveData">

    private final Observer<List<EntityRepositoryInfo>> observerInfo = adRepositoryInfo::setList;

    private final Observer<List<EntityRepositoryGoods>> observerGoods = adRepositoryGoods::setList;

    private final Observer<List<EntityOption>> observerRepository = this::showPickWindow;

    private final Observer<String> observerSelectedRepository = this::updateTitle;

    private final Observer<Boolean> observerCanSelectedRepository = this::canSelectReposity;

    // </editor-fold>

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        Log.e(TAG,"onCreateView"+hashCode());

        return inflater.inflate(R.layout.fragment_navigation_repository,container,false);
    }

    @Override
    protected void initView(@NonNull View view, @NonNull Bundle savedInstanceState) {
        super.initView(view, savedInstanceState);
        Log.e(TAG,"initView:"+view.hashCode());
        rvInfo = ZViewHelp.findById(view,R.id.rvInfo);
        rvGoods = ZViewHelp.findById(view,R.id.rvGoods);
        rvInfo.setLayoutManager(new GridLayoutManager(getContext(),4));
        rvGoods.setLayoutManager(new LinearLayoutManager(getContext()));
        tvTitle = ZViewHelp.setOnClickListener(view,R.id.tvTitle,this);
        ivAdd = ZViewHelp.setOnClickListener(view,R.id.ivAdd,this);


        adRepositoryGoods.setOnItemClickListener(this);
    }

    @Override
    protected void initViewModel() {
        super.initViewModel();
        Log.e(TAG,"initViewModel");

        vmRepository = new ViewModelProvider(this).get(VMRepository.class);

        vmRepository.getLiveInfo().observe(this,observerInfo);

        vmRepository.getLiveGoods().observe(this, observerGoods);

        vmRepository.getLiveRepository().observe(this,observerRepository);

        vmRepository.getLiveSelectRepository().observe(this,observerSelectedRepository);

        vmRepository.getLiveCanSelectRepository().observe(this,observerCanSelectedRepository);


    }

    @Override
    protected void initData() {
        super.initData();

        rvInfo.setAdapter(adRepositoryInfo);

        rvGoods.setAdapter(adRepositoryGoods);

        String name = vmRepository.getLiveSelectRepository().getValue();

        if(name != null){

            updateTitle(name);

        }

        if(UserManager.getInstance().isLogin()){

            if(UserManager.getInstance().getRepository() == null){

                vmRepository.requestRepository();


            }else {

                if (vmRepository.getLiveGoods().getValue() == null) {

                    vmRepository.requestGoods(UserManager.getInstance().getRepository().get(0).getId());
                }
            }

        }


    }

    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.tvTitle){

            v.setEnabled(false);

            vmRepository.requestRepository();
        }


    }

    //<editor-fold defaultState="collapsed" desc="测试">





    // </editor-fold>


    //<editor-fold defaultState="collapsed" desc="私有方法">


    private void showPickWindow(List<EntityOption> list){
        tvTitle.setEnabled(true);
        WindowWheelPick.getInstance()
                .setOptions(list)
                .setOnOptionSelectListener(entityOption -> vmRepository.setSelectedRepository(entityOption))
                .show(getChildFragmentManager(),"");

    }


    private void updateTitle(String title){

        tvTitle.setText(title);

    }

    private void canSelectReposity(Boolean it) {
        tvTitle.setEnabled(it);
    }

    // </editor-fold>

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        ActivityIndex.startACRepositoryGoodsDetail(getActivity(),view.findViewById(R.id.ivFace),
                new BundleGoodsInfo("","","","","",""));
    }

}
